package irri.soils.ssnm;

import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class ActivityW extends SSNM implements TextWatcher{
	private EditText orgN;
	private EditText orgP;
	private EditText orgK;
	private Bundle b;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.w);
        setCustomTitle();

        b = this.getIntent().getExtras();
        orgN = (EditText) findViewById(R.id.orgN);
        orgP = (EditText) findViewById(R.id.orgP);
        orgK = (EditText) findViewById(R.id.orgK);
        DecimalFilter filter = new DecimalFilter();
        
        orgN.setKeyListener(filter);
        orgP.setKeyListener(filter);
        orgK.setKeyListener(filter);
        
        orgN.addTextChangedListener(this);
        orgP.addTextChangedListener(this);
        orgK.addTextChangedListener(this);
        
        if(b.containsKey("orgN")){
        	orgN.setText(""+b.getDouble("orgN"));
        	orgP.setText(""+b.getDouble("orgP"));
        	orgK.setText(""+b.getDouble("orgK"));
        }
        mInputView = (KeyboardView) findViewById(R.id.keyboard);
        
        mInputView.setOnKeyboardActionListener(this);
        mSymbolsKeyboard = new LatinKeyboard(this, R.xml.fraction);
        mInputView.setKeyboard(mSymbolsKeyboard);
    }
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ActivityW.this, ActivityV.class);
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}

	@Override
	public void afterTextChanged(Editable s) {
		String n = ""+orgN.getText();
		String p = ""+orgP.getText();
		String k = ""+orgK.getText();
		double orgNVal = n.equals("") ? 0 : Double.parseDouble(n);
		double orgPVal = p.equals("") ? 0 : Double.parseDouble(p);
		double orgKVal = k.equals("") ? 0 : Double.parseDouble(k);
		if(orgNVal>10){ 
			orgN.setText(""); 
			orgN.append(n.substring(0, n.length()-1));
		}
		else if(orgPVal>10){ 
			orgP.setText(""); 
			orgP.append(p.substring(0, p.length()-1));
		}
		else if(orgKVal>10){ 
			orgK.setText("");
			orgK.append(k.substring(0, k.length()-1));
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}
	
	/* (non-Javadoc)
	 * @see irri.soils.ssnm.SSNMActivity#onKey(int, int[])
	 */
	@Override
	public void onKey(int primaryCode, int[] keyCodes) {
		View v = this.getCurrentFocus();
		try{
			TextView tv = (TextView) v;
			String str = ""+tv.getText();
			
			if (primaryCode == Keyboard.KEYCODE_DELETE) {
				onDelete(tv, str);
			}
			else if(primaryCode == 10){
				if(tv.getId()==orgN.getId()) orgP.requestFocus();
				else if(tv.getId()==orgP.getId()) orgK.requestFocus();
				else{
				Intent intent = new Intent(ActivityW.this, ActivityX.class);
				String n = ""+orgN.getText();
				String p = ""+orgP.getText();
				String k = ""+orgK.getText();
				double orgNVal = n.equals("") ? 0 : Double.parseDouble(n);
				double orgPVal = p.equals("") ? 0 : Double.parseDouble(p);
				double orgKVal = k.equals("") ? 0 : Double.parseDouble(k);
				if(orgNVal<=10&&orgPVal<=10&&orgKVal<=10&&
					(orgNVal>0||orgPVal>0||orgKVal>0)){
					b.putDouble("orgN", orgNVal);
					b.putDouble("orgP", orgPVal);
					b.putDouble("orgK", orgKVal);
					intent.putExtras(b);
					startActivity(intent);
					finish();
				}
				}
			}
			else{
				onDigit(tv, str, primaryCode);
			}
		}catch(Exception ex){
			
		}
	}
}